<template>
  <div class="app-container">
    <!-- 顶部搜索 -->
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch" label-width="110px">
      <el-row :gutter="24">
        <el-col :span="6">
          <el-form-item label="产品/服务名称:" prop="cpfwName">
            <el-select v-model="queryParams.cpfwName" placeholder="请选择产品/服务名称" clearable>
              <el-option
                v-for="dict in dict.type.gqgl_cp_fw_mc"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
<!--        <el-col :span="6">-->
<!--          <el-form-item label="年度:" prop="nd">-->
<!--            <el-input type="number"-->
<!--                      v-model="queryParams.nd"-->
<!--                      placeholder="请输入年度"-->
<!--                      clearable-->
<!--                      @keyup.enter.native="handleQuery"-->
<!--            />-->
<!--          </el-form-item>-->
<!--        </el-col>-->
        <el-col :span="6">
          <el-form-item label="PS编号:" prop="psBh">
            <el-input
              v-model="queryParams.psBh"
              placeholder="请输入PS编号"
              clearable
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <!-- 顶部按钮 -->
    <el-form :model="queryParams" size="mini" label-width="170px">
      <el-row :gutter="10" class="mb8" style="display: flex; justify-content: flex-end;margin-bottom: -8px">
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['gqgl:psb:add']"
            :disabled="!YwJhByGqgl || (YwJhByGqgl !== true)"
          >新增
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['gqgl:psb:edit']"
          >修改
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['gqgl:psb:remove']"
          >删除
          </el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="primary"
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['gqgl:psb:export']"
          >导出
          </el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: auto;">
          <el-form-item label="认定计划:" prop="xmZt">
            <vxe-select @change="getList" size="mini" v-model="queryParams.nd" placeholder="请选择认定计划">
              <vxe-option v-for="item in ywJhList" :key="item.nd" :value="item.nd"
                          :label="item.jhLb"></vxe-option>
            </vxe-select>
          </el-form-item>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </el-form>

    <!-- 表格 -->
    <el-table @row-click="clickRow" ref="table" highlight-current-row
              :header-cell-style="{background:'#d0cece',color:'#000000',fontWeight:'bold'}"
              height="300" class="fixedTableTwo" v-loading="loading" :data="psbList"
              @selection-change="handleSelectionChange">
      <!--      <el-table-column type="selection" width="55" align="center"/>-->
      <el-table-column label="序号" align="center" type="index" :index="indexMethod"/>
      <el-table-column label="PS编号" align="center" prop="psBh"/>
      <el-table-column label="年度" align="center" prop="nd"/>
      <el-table-column label="产品/服务名称" align="center" prop="cpfwName">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gqgl_cp_fw_mc" :value="scope.row.cpfwName"/>
        </template>
      </el-table-column>
      <el-table-column label="技术领域" align="center" prop="jsField">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gqgl_jsly" :value="scope.row.jsField"/>
        </template>
      </el-table-column>
      <el-table-column label="技术来源" align="center" prop="jsSource">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.gqgl_jssource" :value="scope.row.jsSource"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['gqgl:psb:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['gqgl:psb:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改PS对话框 -->
    <vxe-modal :title="title" v-model="open" width="60%" showFooter show-zoom resize>
      <el-form ref="formDialog" :model="form" :rules="rules" label-width="160px">
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="PS编号" prop="psBh">
              <el-input v-model="form.psBh" placeholder="请输入PS编号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="年度" prop="nd">
              <el-input type="number" v-model="form.nd" placeholder="请输入年度"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="产品/服务名称" prop="cpfwName">
              <el-select v-model="form.cpfwName" placeholder="请选择产品/服务名称">
                <el-option
                  v-for="dict in dict.type.gqgl_cp_fw_mc"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="技术领域" prop="jsField">
              <el-select v-model="form.jsField" placeholder="请选择技术领域">
                <el-option
                  v-for="dict in dict.type.gqgl_jsly"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="技术来源" prop="jsSource">
              <el-select v-model="form.jsSource" placeholder="请选择技术来源">
                <el-option
                  v-for="dict in dict.type.gqgl_jssource"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="上年销售收入(万元)" prop="snXsSr">
              <el-input type="number" v-model="form.snXsSr" placeholder="请输入上年销售收入"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="12">
            <el-form-item label="主要产品/服务" prop="zyCpFw">
              <el-select v-model="form.zyCpFw" placeholder="请选择主要产品/服务">
                <el-option
                  v-for="dict in dict.type.sf"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="关键技术及主要技术指标（限4000字）" prop="gjjsJZyjszb">
          <el-input v-model="form.gjjsJZyjszb" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="与同类产品（服务）的竞争优势（限4000字）" prop="ytlcpJzys">
          <el-input v-model="form.ytlcpJzys" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="知识产权获得情况及其对产品（服务）在技术上发挥的支撑作用（限4000字）" prop="zscqHdqk">
          <el-input v-model="form.zscqHdqk" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </vxe-modal>
  </div>
</template>

<script>
import {addPsb, delPsb, getPsb, listPsb, updatePsb} from "@/api/gqgl/psb";
import {selectYwJh} from "@/api/xtgl/ywjh";

export default {
  name: "Psb",
  dicts: ['gqgl_jsly', 'gqgl_jssource', 'gqgl_cp_fw_mc', 'sf'],
  data() {
    return {
      ywJhList: null, //业务计划列表
      YwJhByGqgl: false,
      selectedRow: null, // 保存当前选中的行
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // PS表格数据
      psbList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        psBh: null,
        nd: (new Date().getFullYear()).toString(),
        cpfwName: null,
        idWhere: null,
        userId: null,
        deptId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        psBh: [{required: true, message: "PS编号不能为空", trigger: "blur"}],
        nd: [{required: true, message: "年度不能为空", trigger: "blur"}],
        cpfwName: [{required: true, message: "产品/服务名称不能为空", trigger: "change"}],
        jsField: [{required: true, message: "技术领域不能为空", trigger: "change"}],
        jsSource: [{required: true, message: "技术来源不能为空", trigger: "change"}],
      },
    };
  },
  created() {
    this.getList();
    this.selectYwJhList();
  },
  methods: {
    //点击行 获取行
    clickRow(row) {
      this.selectedRow = row; // 更新选中的行
      const table = this.$refs.table;
      const selectedRows = table.selection;

      // 否则取消之前选中的行，并选中当前点击的行
      selectedRows.forEach(selectedRow => {
        table.toggleRowSelection(selectedRow);
      });
      table.toggleRowSelection(row);
    },
    /** 自定义序号 */
    indexMethod(index) {
      let pageNum = this.queryParams.pageNum - 1;
      if ((pageNum !== -1 && pageNum !== 0)) {
        return (index + 1) + (pageNum * this.queryParams.pageSize);
      } else {
        return (index + 1)
      }
    },
    //不分页查询业务计划中的高企认定计划列表
    selectYwJhList() {
      let query = {
        pageNum: 1,
        pageSize: 50,
        ywSb: '平台资质',
        ywLx: '高企认定',
        zt: '启用'
      }
      selectYwJh(query).then(res => {
        this.ywJhList = res.rows;
      });
    },
    /** 查询PS列表 */
    getList() {
      this.loading = true;
      listPsb(this.queryParams).then(response => {
        let query = {
          pageNum: 1,
          pageSize: 50,
          nd: this.queryParams.nd,
          ywSb: '平台资质',
          ywLx: '高企认定',
        }
        selectYwJh(query).then(res => {
          let ywjhList = res.rows;
          if (ywjhList[0].jhZt == '进行中') {
            this.YwJhByGqgl = true
          } else {
            this.YwJhByGqgl = false
          }

          this.psbList = response.rows;
          this.total = response.total;
          this.loading = false;
        });


      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        psbId: null,
        psBh: null,
        nd: null,
        cpfwName: null,
        jsField: null,
        jsSource: null,
        snXsSr: null,
        zyCpFw: null,
        gjjsJZyjszb: null,
        ytlcpJzys: null,
        zscqHdqk: null,
        idWhere: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: null,
        deptId: null
      };
      this.resetForm("formDialog");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.psbId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加PS";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const psbId = row.psbId || this.ids
      getPsb(psbId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改PS";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["formDialog"].validate(valid => {
        if (valid) {
          if (this.form.idWhere != null) {
            updatePsb(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPsb(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const psbIds = row.psbId || this.ids;
      this.$modal.confirm('确定删除选中记录？').then(function () {
        return delPsb(psbIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gqgl/psb/export', {
        ...this.queryParams
      }, `psb_${new Date().getTime()}.xlsx`)
    },
  }
};
</script>
